package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.FacilityInfo;
import com.atguigu.model.enums.ItemType;
import com.atguigu.model.pojo.LabelInfo;
import com.atguigu.webadmin.service.FacilityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "配套管理")
@RestController
@RequestMapping("/admin/facility")
@Slf4j
@CrossOrigin
public class FacilityController {

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Operation(summary = "[根据类型]查询配套信息列表")
    @Parameters({
            @Parameter(name = "type",description = "房间类型",in = ParameterIn.QUERY)
    })
    @GetMapping("list")
    public Result<List<FacilityInfo>> listFacility(@RequestParam(required = false) ItemType type) {
        //使用MybatisPlus的条件查询
        //LambdaQueryWrapper:Lambda查询包装器
        LambdaQueryWrapper<FacilityInfo> queryWrapper = new LambdaQueryWrapper<>();
        //条件：标签类型：先判空，再设定查询条件（FacilityInfo::getType：对象中的任意元素）
        queryWrapper.eq(type != null,FacilityInfo::getType,type);
        //根据新条件进行查询
        List<FacilityInfo> facilityInfos = facilityInfoService.list(queryWrapper);
        //返回结果
        return Result.ok(facilityInfos);
    }

    @Operation(summary = "新增或修改配套信息")
    @Parameters({
            @Parameter(name = "facilityInfo",description = "需要新增或修改的配套信息对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody FacilityInfo facilityInfo) {
        //新增或修改配套信息
        boolean b = facilityInfoService.saveOrUpdate(facilityInfo);
        return Result.ok(b);
    }

    @Operation(summary = "根据id删除配套信息")
    @Parameters({
            @Parameter(name = "id",description = "配套信息id",in = ParameterIn.DEFAULT)
    })
    @DeleteMapping("deleteById")
    public Result<Boolean> removeFacilityById(@RequestParam Long id) {
        //根据id逻辑删除配套信息
        boolean b = facilityInfoService.removeById(id);
        //返回结果
        return Result.ok(b);
    }

}
